package com.my.system.dao.customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.my.system.dao.DataBaseConnection;
import com.my.system.object.client.ClientStatus;

public class ClientStatusDao {
	private static Connection con = null;
	private static PreparedStatement psta = null;
	private static ResultSet rs = null;
	static {
		con = DataBaseConnection.getConnection();
	}

	public static ClientStatus searchClientStatus(String code) {
		String sql = "select * from client_status where code=?";
		ClientStatus replace = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, code);
			rs = psta.executeQuery();
			if (rs.next()) {
				int id = rs.getInt(1);
				String code1 = rs.getString(2);
				String name = rs.getString(3);
				String description = rs.getString(4);
				replace = new ClientStatus(id, code1, name, description);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return replace;
	}

	public static boolean insertClientStatus(ClientStatus clentStatus) {
		String sql = "insert into client_status(" + "code," + "name,"
				+ "description)" + "values(?,?,?)";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, clentStatus.getCode());
			psta.setString(2, clentStatus.getName());
			psta.setString(3, clentStatus.getDescription());

			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	public static List<ClientStatus> searchAllType() {
		List<ClientStatus> list = new ArrayList<ClientStatus>();
		String sql = "select * from client_status ";
		try {
			psta = con.prepareStatement(sql);
			rs = psta.executeQuery();
			while (rs.next()) {
				 int id=rs.getInt(1);
				 String code=rs.getString(2);
				 String name=rs.getString(3);
				 String description=rs.getString(4);
				 ClientStatus type=new ClientStatus(id, code, name, description);
				 list.add(type);

			}
			if (list.size() > 0) {
				return list;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}
}
